from fastapi import APIRouter, Query
from server.controllers.user import get_db, Depends, get_current_user
from server.models.user import User
from server.schemas.achievement import AchievementBase
from server.schemas.review import SubmittedFormQuery, SubmittedFormContentQuery
from server.schemas.user import APIResponse
from server.controllers.review import *
from log import logger
from server.database import Base, engine
from server.models import success_response, error_response

Base.metadata.create_all(bind=engine)
router = APIRouter()


@router.get("/review/achievement/list", tags=["review", "get"])
async def _get_achievement_list(db: Session = Depends(get_db), user: User = Depends(get_current_user),
                                form: bool = Query(title="是否获取填写的信息", default=True)) -> APIResponse:
    """获取成果表，并判断是否已经填写"""
    try:
        achievement_list = get_achievement_list(db, user.id if form else None)
        return success_response(achievement_list)
    except ProjectException as e:
        logger.error(e)
        return error_response(str(e))
    except Exception as e:
        logger.exception(e)
        return error_response("服务器错误，请重试", 500)


@router.post("/review/achievement_rules/get", tags=["review", "post"])
async def _get_achievement_info_and_rules(query_achievement_rules: AchievementBase, db: Session = Depends(get_db),
                                          user: User = Depends(get_current_user)) -> APIResponse:
    """获取指定id的成果规则表"""
    try:
        achievement_info = get_achievement_info_and_rules(db, query_achievement_rules.id)
        return success_response(achievement_info)
    except ProjectException as e:
        logger.error(e)
        return error_response(str(e))
    except Exception as e:
        logger.exception(e)
        return error_response("服务器错误，请重试", 500)


@router.get("/review/file/upload", tags=["review", "get"])
async def _get_upload_sign(file_name: str = Query(title="文件名"), db: Session = Depends(get_db),
                           user: User = Depends(get_current_user)) -> APIResponse:
    """获取腾讯云对象储存上传URL和签名"""
    try:
        data = get_upload_sign(file_name)
        return success_response(data)
    except ProjectException as e:
        logger.error(e)
        return error_response(str(e))
    except Exception as e:
        logger.exception(e)
        return error_response("服务器错误，请重试", 500)


@router.get("/review/file/download", tags=["review", "get"])
async def _get_upload_sign(file_name: str = Query(title="文件名"), db: Session = Depends(get_db),
                           user: User = Depends(get_current_user)) -> APIResponse:
    """获取腾讯云对象储存下载URL和签名"""
    try:
        data = get_download_sign(file_name)
        return success_response(data)
    except ProjectException as e:
        logger.error(e)
        return error_response(str(e))
    except Exception as e:
        logger.exception(e)
        return error_response("服务器错误，请重试", 500)


@router.post("/review/achievement_form/submit", tags=["review", "post"])
async def _submit_form(form_data: SubmittedFormUserCreate, db: Session = Depends(get_db),
                       user: User = Depends(get_current_user)) -> APIResponse:
    """创建提交成果表，并将上传内容"""
    try:
        if any(achievement.get('id') == form_data.achievement_id and achievement.get("submitted_form") for achievement
               in get_achievement_list(db, user.id)):
            return error_response("该表单已经提交")
        create_submitted_form(db, form_data, user.id)
        return success_response()
    except ProjectException as e:
        logger.error(e)
        return error_response(str(e))
    except Exception as e:
        logger.exception(e)
        return error_response("服务器错误，请重试", 500)


@router.post("/review/achievement_content/list", tags=["review", "post"])
async def _get_submit_content_form(achievement_base: AchievementBase, db: Session = Depends(get_db),
                                   user: User = Depends(get_current_user)) -> APIResponse:
    """获取与指定 user_id 和 achievement_id 关联的所有 SubmittedForm"""
    try:
        data = get_user_submitted_form_contents_by_achievement_id(db, achievement_base.id, user.id)
        return success_response(data if data else None)
    except ProjectException as e:
        logger.error(e)
        return error_response(str(e))
    except Exception as e:
        logger.exception(e)
        return error_response("服务器错误，请重试", 500)


@router.post("/review/achievement_form/info", tags=["review", "post"])
async def _get_submit_form_info(achievement_base: SubmittedFormQuery, db: Session = Depends(get_db),
                                user: User = Depends(get_current_user)) -> APIResponse:
    """查询SubmittedForm表以及对应的用户信息"""
    try:
        data = get_submitted_form_and_info(db, achievement_base)
        return success_response(data if data else None)
    except ProjectException as e:
        logger.error(e)
        return error_response(str(e))
    except Exception as e:
        logger.exception(e)
        return error_response("服务器错误，请重试", 500)


@router.post("/review/achievement_form_content/info", tags=["review", "post"])
async def _get_achievement_form_content_info(achievement_base: SubmittedFormContentQuery, db: Session = Depends(get_db),
                                             user: User = Depends(get_current_user)) -> APIResponse:
    """选定指定submitted_form并获取指定信息"""
    try:
        data = select_submitted_form_by_id(db, achievement_base.id, user.id)
        return success_response(data if data else None)
    except ProjectException as e:
        logger.error(e)
        return error_response(str(e))
    except Exception as e:
        logger.exception(e)
        return error_response("服务器错误，请重试", 500)


@router.post("/review/achievement_form_content/review", tags=["review", "post"])
async def _submit_form(params: SubmittedReview, db: Session = Depends(get_db),
                       user: User = Depends(get_current_user)) -> APIResponse:
    """提交审核内容"""
    try:
        submit_review(db, params, user.id)
        return success_response()
    except ProjectException as e:
        logger.error(e)
        return error_response(str(e))
    except Exception as e:
        logger.exception(e)
        return error_response("服务器错误，请重试", 500)
